﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Reg()
        {
            return View();
        }
        public ActionResult Login()
        {
            if (Session["Info"] == null)
            {
                return View();
            }
            else
            {
                if (Session["Info"].ToString() == "User")
                {
                    return Redirect("/User/Home");
                }
                else
                {
                    return View();
                }
            }
            
        }
        public ActionResult UserLogin()
        {
            string username = Request.Form["UserName"].ToString();
            string password = Request.Form["Password"].ToString();
            UserInfo user = entities.UserInfos.FirstOrDefault(o => o.UserName == username && o.UserPassword == password);
            Message message = new Message { Result = false, Messages = "用户名或者密码错误" };
            if (user == null)
            {
                
                return Json(message);
            }
            else
            {
                Session["Info"] = "User";
                Session["ID"] = user.ID;
                Session["UserName"] = user.UserName;
                message.Result = true;
                user.Point = user.Point + 10;
                entities.SaveChanges();
            }
            return Json(message);
        }
        public ActionResult CheckUser()
        {
            string username = Request.Form["UserName"].ToString();
            UserInfo user = entities.UserInfos.FirstOrDefault(o => o.UserName == username);
            Message message = new Message { Result = false, Messages = "用户名已经存在，请更换其他用户名" };
            if (user == null)
            {
                message.Result = true;
                message.Messages = "用户名可以使用;";
            }
            return Json(message, JsonRequestBehavior.AllowGet);
        
        }
        public ActionResult CheckEmail()
        {
            string email = Request.Form["Email"].ToString();
            UserInfo user = entities.UserInfos.FirstOrDefault(o => o.Email == email.ToLower());
            Message message = new Message { Result = false, Messages = "用户名已经存在，请更换其他用户名" };
            if (user == null)
            {
                message.Result = true;
                message.Messages = "用户名可以使用;";
            }
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult RegUser()
        {
            
            Message message=new Message{Result=false,Messages="用户名已经存在，请更换用户名！"};
            string username = Request.Form["UserName"].ToString();
            string password = Request.Form["Password"].ToString();
            string enterpassword = Request.Form["EnterPassword"].ToString();
            string email = Request.Form["Email"].ToString();
              UserInfo user = entities.UserInfos.FirstOrDefault(o => o.UserName == username);
              if (user == null)
              {
                  user = entities.UserInfos.FirstOrDefault(o => o.Email == email.ToLower());
                  if (user == null)
                  {
                      user = new UserInfo();
                      message.Result = true;
                      user.Email = email;
                      user.UserName = username;
                      user.UserPassword = password;
                      user.Point = Convert.ToInt32(UcAsp.WorkFrame.SettingConfig.GetSiteSetting.PointsRate);
                      user.SessionID = Session.SessionID;
                      user.Logo = UcAsp.WorkFrame.SettingConfig.GetSiteSetting.UserHeader;
                      user.LoginTime = DateTime.Now;
                      user.LoginIP = Request.UserHostAddress;
                      user.RecommendID = 0;
                      entities.UserInfos.Add(user);
                      int id=entities.SaveChanges();
                      Session["Info"] = "User";
                      Session["ID"] = id.ToString();
                      Session["UserName"] = user.UserName;
                      return  Json(message, JsonRequestBehavior.AllowGet);
                  }
                  else
                  {
                      message.Messages = "Email已经注册，请跟换其他Email";
                      return Json(message, JsonRequestBehavior.AllowGet);
                  }
              }
              else
              {
                  return Json(message, JsonRequestBehavior.AllowGet);
              }
        }
        public ActionResult Home()
        {
            if (Session["Info"] == null)
            {
                return Redirect("/User/Login");
            }
            else
            {
                if (Session["Info"].ToString() == "User")
                {
                    int ID = int.Parse(Session["ID"].ToString());
                    UserInfo info = entities.UserInfos.FirstOrDefault(o => o.ID == ID);
                    return View(info);
                }
                else
                {
                    return Redirect("/User/Login");
                }
            }
            
        }
        public ActionResult SaveComments()
        {
            int userid = 1;
            string username="悠点网友";
            int artid=int.Parse(Request.Form["ID"]);
            if (Session["ID"] != null)
            {
                userid = int.Parse(Session["ID"].ToString());
                username= Session["UserName"].ToString();
            }
            string publichcontent = Request.Form["PublichContent"];
            string title = Request.Form["Title"];
            int parentid=1;
            if (Request.Form["ParentID"] != null)
            {
                parentid = int.Parse(Request.Form["ParentID"]);
            }
            Comments comments = new Comments { ArticleID = artid, UserName = username, ParentID = parentid, UserID = userid, PublicDate = DateTime.Now, PublichContent = publichcontent, Title = title, ReplyDateTime = DateTime.Now };
            entities.Commentss.Add(comments);
            entities.SaveChanges();
            Message message = new Message { Result = true };
           return  Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SaveReplay()
        { 
            int userid = 99999;
            string username="悠点";
            if (Session["ID"] != null)
            {
                userid = int.Parse(Session["ID"].ToString());
                username= Session["UserName"].ToString();
            }
            string publichcontent = Request.Form["PublichContent"];
            string title = Request.Form["Title"];
            int parentid=0;
            if (Request.Form["ParentID"] != null)
            {
                parentid = int.Parse(Request.Form["ParentID"]);
            }
            Comments comments = new Comments { ArticleID = 0, ParentID = parentid, UserName=username, UserID = userid, PublicDate = DateTime.Now, PublichContent = publichcontent, Title = title, ReplyDateTime=DateTime.Now };
            Message message = new Message { Result = true };
           return  Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult QianMing() { 
            if (Session["Info"] == null)
            {
                return Redirect("/User/Login");
            }
            else
            {
                if (Session["Info"].ToString() == "User")
                {
                    int ID = int.Parse(Session["ID"].ToString());
                    UserInfo info = entities.UserInfos.FirstOrDefault(o => o.ID == ID);
                    return View(info);
                }
                else
                {
                    return Redirect("/User/Login");
                }
            }
        }
        public ActionResult SaveQm()
        { 
            string Remark=Request.Form["Remark"];
            Message message = new Message { Result = true, Messages = Remark };
            int ID = int.Parse(Session["ID"].ToString());
            UserInfo info = entities.UserInfos.FirstOrDefault(o => o.ID == ID);
            info.Remark = Remark;
            entities.SaveChanges();
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult UpHeader()
        {
            return View();
        }
        public ActionResult SaveHeader()
        {
            string Logo = Request.Form["Logo"];
            Message message = new Message { Result = true, Messages = Logo };
            int ID = int.Parse(Session["ID"].ToString());
            UserInfo info = entities.UserInfos.FirstOrDefault(o => o.ID == ID);
            info.Logo = Logo;
            entities.SaveChanges();
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Point()
        {
            int ID = int.Parse(Session["ID"].ToString());
            UserInfo user = entities.UserInfos.FirstOrDefault(o => o.ID == ID);
            return PartialView(user);
        }
    }
}
